/*
 * ZapytanieGUI.java
 *
 * Created on 3 grudzień 2007, 19:48
 */
package pl.cezary.butler.walili.client.gui;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import pl.cezary.butler.walili.common.Task;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.AbstractTableModel;
import pl.cezary.butler.walili.common.TaskStatus;

/**
 *
 * @author neohuman
 */
//Musiałen wsócić do listy stringów bo jeśli jest pusta lista nie mogłem się pozbyć
//nullexeption
public class ZapytanieGUI extends javax.swing.JPanel {

    private static final Logger log = Logger.getLogger(ZapytanieGUI.class.getName());

    public static class ModelDanychZadanie extends AbstractTableModel {

        private final String[] columns = {
            GUI.getLocalized("query.location"),
            GUI.getLocalized("query.target"),
            GUI.getLocalized("query.status"),
            GUI.getLocalized("query.mime"),
        GUI.getLocalized("query.priority")};
        private List<Task> zadania = null;

        public ModelDanychZadanie() {
        }

        public void setZadania(List<Task> z) {
            zadania = z;
        }

        @Override
        public int getRowCount() {
            return (zadania != null) ? zadania.size() : 0;
        }

        @Override
        public int getColumnCount() {
            return columns.length;
        }

        @Override
        public String getColumnName(int col) {
            return columns[col];
        }

        @Override
        public String getValueAt(int rowIndex, int columnIndex) {
            try {
                switch (columnIndex) {
                    case 0:
                        final String polozenie = zadania.get(rowIndex).getPolozenie();
                        return (polozenie != null) ? URLDecoder.decode(polozenie, "UTF-8") : "";
                    case 1:
                        final String cel = zadania.get(rowIndex).getCel();
                        return (cel != null) ? URLDecoder.decode(cel, "UTF-8") : "";
                    case 2:
                        return zadania.get(rowIndex).getStatus().name();
                    case 3:
                        return zadania.get(rowIndex).getMimeType();
                    case 4:
                        return Integer.toString(zadania.get(rowIndex).getPriorytet());
                    default:
                        return null;
                }
            } catch (UnsupportedEncodingException ie) {
                log.log(Level.SEVERE, null, ie);
                throw new RuntimeException(ie);

            }
        }
    }
    
    private final DefaultComboBoxModel model = new DefaultComboBoxModel(Status);
    private final ModelDanychZadanie modelWynikow = new ModelDanychZadanie();
    
    private static final String[] Status = {        
        GUI.getLocalized("query.status.success"),
        GUI.getLocalized("query.status.fail"),
        GUI.getLocalized("query.status.new"),
        GUI.getLocalized("query.status.interrupted"),
        GUI.getLocalized("query.status.renew"),
        GUI.getLocalized("query.status.match_all")};
    
    public TaskStatus getQueryStatus() {
        switch (jComboBoxStatus.getSelectedIndex()) {
            case 0:
                return TaskStatus.SUKCES;
            case 1:
                return TaskStatus.PORAZKA;
            case 2:
                return TaskStatus.NOWE;
            case 3:
                return TaskStatus.PRZERWANE;
            case 4:
                return TaskStatus.WZNOWIONE;
            default:
                return null;
        }
    }       
    
    public void setResults(final List<Task> res){
        modelWynikow.setZadania(res);                
        jTableWyniki.revalidate();
        jTableWyniki.repaint();
    }

    /**
     * Creates new form ZapytanieGUI
     */
    public ZapytanieGUI() {
        initComponents();
    }
    
    public String getQueryTarget(){
        return jTextFieldCel.getText();
    }
    
    public String getQueryLocation(){
        return jTextFieldPolozenie.getText();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroupStatus = new javax.swing.ButtonGroup();
        jLabelPolozenie = new javax.swing.JLabel();
        jLabelCel = new javax.swing.JLabel();
        jLabelStatus = new javax.swing.JLabel();
        jButtonZapytanie = new javax.swing.JButton();
        jScrollPaneWyniki = new javax.swing.JScrollPane();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui"); // NOI18N
        setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("query.query"))); // NOI18N
        setMaximumSize(new java.awt.Dimension(412, 235));
        setMinimumSize(new java.awt.Dimension(412, 235));
        setPreferredSize(new java.awt.Dimension(412, 235));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        jPanelPolozenie.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanelPolozenie.setMaximumSize(new java.awt.Dimension(32767, 90));
        jPanelPolozenie.setMinimumSize(new java.awt.Dimension(500, 65));
        jPanelPolozenie.setPreferredSize(new java.awt.Dimension(500, 65));
        jPanelPolozenie.setRequestFocusEnabled(false);
        java.awt.GridBagLayout jPanelPolozenieLayout = new java.awt.GridBagLayout();
        jPanelPolozenieLayout.columnWidths = new int[] {0, 4, 0};
        jPanelPolozenieLayout.rowHeights = new int[] {0, 15, 0};
        jPanelPolozenie.setLayout(jPanelPolozenieLayout);

        jLabelPolozenie.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelPolozenie.setText(bundle.getString("query.location")); // NOI18N
        jLabelPolozenie.setMaximumSize(new java.awt.Dimension(55, 14));
        jLabelPolozenie.setMinimumSize(new java.awt.Dimension(55, 14));
        jLabelPolozenie.setPreferredSize(new java.awt.Dimension(55, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
        jPanelPolozenie.add(jLabelPolozenie, gridBagConstraints);

        jTextFieldPolozenie.setMaximumSize(new java.awt.Dimension(1000, 20));
        jTextFieldPolozenie.setMinimumSize(new java.awt.Dimension(180, 20));
        jTextFieldPolozenie.setPreferredSize(new java.awt.Dimension(250, 20));
        jTextFieldPolozenie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldPolozenieActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        jPanelPolozenie.add(jTextFieldPolozenie, gridBagConstraints);

        jLabelCel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelCel.setText(bundle.getString("query.target")); // NOI18N
        jLabelCel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabelCel.setMaximumSize(new java.awt.Dimension(55, 14));
        jLabelCel.setMinimumSize(new java.awt.Dimension(55, 14));
        jLabelCel.setPreferredSize(new java.awt.Dimension(55, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
        jPanelPolozenie.add(jLabelCel, gridBagConstraints);

        jTextFieldCel.setMaximumSize(new java.awt.Dimension(1000, 20));
        jTextFieldCel.setMinimumSize(new java.awt.Dimension(180, 20));
        jTextFieldCel.setPreferredSize(new java.awt.Dimension(250, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        jPanelPolozenie.add(jTextFieldCel, gridBagConstraints);

        add(jPanelPolozenie);

        jPanelStatus.setMaximumSize(new java.awt.Dimension(32767, 35));
        jPanelStatus.setMinimumSize(new java.awt.Dimension(412, 35));
        jPanelStatus.setPreferredSize(new java.awt.Dimension(412, 35));
        jPanelStatus.setLayout(new java.awt.GridBagLayout());

        jLabelStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelStatus.setText(bundle.getString("query.status")); // NOI18N
        jLabelStatus.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabelStatus.setMaximumSize(new java.awt.Dimension(45, 14));
        jLabelStatus.setMinimumSize(new java.awt.Dimension(45, 14));
        jLabelStatus.setPreferredSize(new java.awt.Dimension(45, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 5;
        jPanelStatus.add(jLabelStatus, gridBagConstraints);

        jComboBoxStatus.setModel(model);
        jPanelStatus.add(jComboBoxStatus, new java.awt.GridBagConstraints());

        jButtonZapytanie.setText(bundle.getString("query.send")); // NOI18N
        jButtonZapytanie.setAlignmentY(0.0F);
        jPanelStatus.add(jButtonZapytanie, new java.awt.GridBagConstraints());

        add(jPanelStatus);

        jTableWyniki.setModel(modelWynikow);
        jTableWyniki.setFillsViewportHeight(true);
        jTableWyniki.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        jScrollPaneWyniki.setViewportView(jTableWyniki);

        add(jScrollPaneWyniki);
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldPolozenieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPolozenieActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldPolozenieActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupStatus;
    public javax.swing.JButton jButtonZapytanie;
    private final javax.swing.JComboBox jComboBoxStatus = new javax.swing.JComboBox();
    private javax.swing.JLabel jLabelCel;
    private javax.swing.JLabel jLabelPolozenie;
    private javax.swing.JLabel jLabelStatus;
    private final javax.swing.JPanel jPanelPolozenie = new javax.swing.JPanel();
    private final javax.swing.JPanel jPanelStatus = new javax.swing.JPanel();
    private javax.swing.JScrollPane jScrollPaneWyniki;
    private final javax.swing.JTable jTableWyniki = new javax.swing.JTable();
    private final javax.swing.JTextField jTextFieldCel = new javax.swing.JTextField();
    private final javax.swing.JTextField jTextFieldPolozenie = new javax.swing.JTextField();
    // End of variables declaration//GEN-END:variables
}
